package com.estronger.shopmanage.ui.home;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.widget.ImageView;

import com.estronger.shopmanage.R;
import com.estronger.shopmanage.base.MyApplication;
import com.estronger.shopmanage.bean.B1Login;
import com.estronger.shopmanage.ui.MainActivity;
import com.estronger.shopmanage.utils.MD5Utils;
import com.estronger.shopmanage.utils.PrefUtils;
import com.estronger.shopmanage.utils.Toast;
import com.google.gson.Gson;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.orhanobut.hawk.Hawk;
import com.soonfor.evaluate.tools.CommonUtils;
import com.soonfor.evaluate.tools.UserInfo;
import com.soonfor.systemcodes.SystemCodes;
import com.squareup.picasso.Picasso;


public class FirstActivity extends Activity {

    private MyApplication app;
    ImageView ads_img;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.first_page);
        app = MyApplication.getApplication();
        ads_img = (ImageView) findViewById(R.id.ads_img);
        SystemCodes.getInstance().initValues();
    }

    @Override
    protected void onResume() {
        super.onResume();
        final String firsturl = PrefUtils.getString(FirstActivity.this, "firstPic", "");
        if (!firsturl.equals("")) {
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    Picasso.with(FirstActivity.this).load(firsturl).into(ads_img);
                    new Handler().postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            setLoginOrOther();
                        }
                    }, 1500);
                }
            }, 1500);
        } else {
            setLoginOrOther();
        }
    }

    private void setLoginOrOther() {
        if (PrefUtils.getBoolean(FirstActivity.this, "isLogin", false)) {
            final String userName = PrefUtils.getString(this, "userName", "");
            final String userpwd = PrefUtils.getString(this, "userPwd", "");
            if (!userName.equals("") && !userpwd.equals("")) {
                RequestParams params = new RequestParams();
                params.addBodyParameter("regid", CommonUtils.getIMEI(FirstActivity.this));
                params.addBodyParameter("usercode", userName);
                params.addBodyParameter("password", MD5Utils.md5Password(userpwd));
                params.addBodyParameter("sf_action", "login");
                String url = MyApplication.getApplication().getSERVER_URL()
                        + "/web/common/API/apphandler.ashx";
                HttpUtils httpUtils = new HttpUtils();
                httpUtils.configSoTimeout(15 * 1000);
                httpUtils.send(HttpRequest.HttpMethod.POST, url, params, new RequestCallBack<String>() {
                    @Override
                    public void onSuccess(ResponseInfo<String> responseInfo) {
                        try {
                            MyApplication.login = new Gson().fromJson(responseInfo.result, B1Login.class);
                            if (MyApplication.login.success) {
                                PrefUtils.setBoolean(FirstActivity.this, "isLogin", true);
                                PrefUtils.setString(FirstActivity.this, "loginData", responseInfo.result);
                                PrefUtils.setString(FirstActivity.this, "userName", userName);
                                PrefUtils.setString(FirstActivity.this, "userPwd", userpwd);
                                Hawk.put(UserInfo.LOGIN_USERINFO, MyApplication.login.data);
                                startActivity(new Intent(FirstActivity.this, MainActivity.class));
                                finish();
                            } else {
                                if (MyApplication.login.msg != null && !MyApplication.login.msg.equals("")) {
                                    Toast.show(FirstActivity.this, MyApplication.login.msg, Toast.LENGTH_SHORT);
                                }
                                startActivity(new Intent(FirstActivity.this,
                                        LoginActivity.class));
                                finish();
                            }
                        } catch (Exception e) {
                            Toast.show(FirstActivity.this, "自动登录出错！", Toast.LENGTH_SHORT);
                            startActivity(new Intent(FirstActivity.this, LoginActivity.class));
                            finish();
                        }
                    }

                    @Override
                    public void onFailure(HttpException e, String s) {
                        startActivity(new Intent(FirstActivity.this,
                                LoginActivity.class));
                        finish();
                    }
                });
            } else {
                startActivity(new Intent(FirstActivity.this,
                        LoginActivity.class));
                finish();
            }
        } else if (PrefUtils.getBoolean(FirstActivity.this, "is_user_guide_showed", false)) {//非第一次进入app
            if (!app.getSERVER_URL().equals("")) {
                startActivity(new Intent(FirstActivity.this,
                        LoginActivity.class));
                finish();
            } else {
                startActivity(new Intent(FirstActivity.this,
                        ServerActivity.class));
                finish();
            }
        } else {//第一次安装进入app
            startActivity(new Intent(FirstActivity.this,
                    GuideActivity.class));
            finish();
        }
    }

}
